package com.kbbywyclient.ui.fragment.tab;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.LoanIndex;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.MainModule;
import com.kbbywyclient.presenter.MainPresenter;
import com.kbbywyclient.ui.activity.CommonQuestionActivity;
import com.kbbywyclient.ui.activity.LoginActivity;
import com.kbbywyclient.ui.activity.MineLoanActivity;
import com.kbbywyclient.ui.activity.MinePromotionActivity;
import com.kbbywyclient.ui.activity.ModifyInfoActivity;
import com.kbbywyclient.ui.activity.SettingActivity;
import com.kbbywyclient.ui.contarct.MainContract;
import com.kbbywyclient.ui.fragment.base.BaseFragment;
import com.kbbywyclient.util.ActivitySkipUtil;
import com.kbbywyclient.util.ImageLoaderUtils;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.util.pre.PreUtils;
import com.kbbywyclient.widget.ptr.PullRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.HashMap;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Jiangzx on 2017/8/15 14:09.
 */

public class MineFragment extends BaseFragment implements MainContract.View {
    //公共的
    @BindView(R.id.ptr_layout)
    PtrClassicFrameLayout ptr_layout;

    @BindView(R.id.iv_head)
    CircleImageView iv_head;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_company)
    TextView tv_company;

    private int mType = 0;//0-贷款业务 1-信用卡业务

    private String goToType = "";


    @Inject
    MainPresenter mainPresenter;

    private int activation = -1;

    private Activity activity;

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_mine;
    }

    @OnClick({R.id.ibt_setting,R.id.ibt_question,
            R.id.ll_user_info,
            R.id.rl_mine_promotion,R.id.rl_mine_loan})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibt_setting:

                if (PreUtils.isLogin(activity)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("activation", activation);
                    map.put("mType", mType);
                    map.put("goToType", goToType);
                    ActivitySkipUtil.skipAnotherActivity(activity, SettingActivity.class, map);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(activity, LoginActivity.class);
                }
                break;
            case R.id.ibt_question:
                // 常见问题

                ActivitySkipUtil.skipAnotherActivity(activity, CommonQuestionActivity.class);
                break;
            case R.id.ll_user_info://基本资料
                if (PreUtils.isLogin(activity)) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("activation", activation);
                    ActivitySkipUtil.skipAnotherActivity(activity, ModifyInfoActivity.class, map);
                } else {
                    ActivitySkipUtil.skipAnotherActivity(activity, LoginActivity.class);
                }
                break;
            case R.id.rl_mine_promotion:
                //我的推广
                if(PreUtils.isLogin(activity)){
                    ActivitySkipUtil.skipAnotherActivity(activity, MinePromotionActivity.class);
                }else{
                    ActivitySkipUtil.skipAnotherActivity(activity,LoginActivity.class);
                }
                break;
            case R.id.rl_mine_loan:
                //我的贷款
                if(PreUtils.isLogin(activity)){
                    ActivitySkipUtil.skipAnotherActivity(activity, MineLoanActivity.class);
                }else{
                    ActivitySkipUtil.skipAnotherActivity(activity,LoginActivity.class);
                }
                break;
        }
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);

        activity = getActivity();
//        mType = PreUtils.getBusinessId(getContext());

        PullRefreshLayout header = new PullRefreshLayout(getContext());
        header.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                StringUtil.dip2px(getContext(), 45)));
        ptr_layout.setHeaderView(header);
        ptr_layout.addPtrUIHandler(header);
        ptr_layout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                showBusinessView();
            }
        });
        ptr_layout.disableWhenHorizontalMove(true);

    }

    //处理用户登出后昵称、头像的设置
    private void onUserLoginOut() {
        iv_head.setImageResource(R.mipmap.icon_head_portrait);
        tvPhone.setVisibility(View.VISIBLE);
        ViewUtils.setText(tvPhone, "未登录");
        tv_name.setVisibility(View.GONE);
        tv_company.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        if (!PreUtils.isLogin(getContext())) {
            onUserLoginOut();
        }
        showBusinessView();
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getMainComponent(new MainModule(this)).inject(this);//注入view
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
        ptr_layout.refreshComplete();
    }

    @Override
    public void loadSucLoanIndex(LoanIndex loanIndex) {
        Context context=getContext();
        if (loanIndex.getCity() != null) {
            if (StringUtil.isNotEmpty(loanIndex.getCity().toString())) {
                PreUtils.setCity(context, Integer.valueOf(new DecimalFormat("0").format(loanIndex.getCity())));
            }
        }

        goToType = loanIndex.getGoToType();
        activation = loanIndex.getActivation();
        PreUtils.setCertificationStatus(context,activation);

        PreUtils.setCompanyID(context,String.valueOf(loanIndex.getCompanyID()));
        PreUtils.setCompanyTypeID(context,String.valueOf(loanIndex.getCompanyTypeID()));

        if(!TextUtils.isEmpty(loanIndex.getLogo())){
            PreUtils.setAvatar(context,loanIndex.getLogo());
        }
        ImageLoaderUtils.displayImageDefaultRes(iv_head,
                API.PIC_ADDRESS+loanIndex.getLogo(),
                R.mipmap.icon_head_portrait);

        if(1==loanIndex.getActivation()){
            //已激活
            PreUtils.setTrueName(context,loanIndex.getTrueName());
            tvPhone.setVisibility(View.GONE);
            tv_name.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_name, loanIndex.getTrueName());
            tv_company.setVisibility(View.VISIBLE);
            ViewUtils.setText(tv_company, loanIndex.getNameShort());
        }else{
            PreUtils.setTrueName(context,"");
            tvPhone.setVisibility(View.VISIBLE);
            tvPhone.setText(PreUtils.getUserAccount(getContext()));
            tv_name.setVisibility(View.GONE);
            tv_company.setVisibility(View.GONE);
        }
    }


//    @Override
//    public void loadFaiLoanIndex(String errorMsg) {
//
//    }


//    @Override
//    public void loadFaiCreditIndex(String errorMsg) {
//
//    }

    @Override
    public void finishRefresh() {
        if (ptr_layout.isRefreshing()) {
            ptr_layout.refreshComplete();
        }
    }

    private void showBusinessView() {
//        switch (PreUtils.getBusinessId(getContext())) {
//            case 0://贷款
//                setLoanView();
                if (PreUtils.isLogin(getContext())) {
                    mainPresenter.getLoanIndex(getUser().getSalesID() + "");
                } else {
                    ptr_layout.refreshComplete();
                }
//                break;
//            case 1://信用卡
//                setCreditView();
//                if (PreUtils.isLogin(getContext())) {
//                    mainPresenter.getCreditCardIndex(getUser().getSalesID() + "");
//                } else {
//                    ptr_layout.refreshComplete();
//                }
//                break;
//        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAccountSignIn(Event.AccountSignIn signIn){
        //账号登入
        showBusinessView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAccountSignOut(Event.AccountSignOut signOut){
        //账号登出
        onUserLoginOut();
    }
}
